import javax.swing.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import java.util.jar.JarOutputStream;

public class ATMMore{
    public Properties pro = new Properties();
    public ATMMoreData loginCount;

    public ATMMore(){
        try {
            pro.load(new FileReader("Class8_25/src/ATMMore.txt"));
        }catch (Exception e){
            System.out.println("文件不存在");
        }
    }

    public void login(){
        for (int i=0;i<3;i++){
            String inCount = JOptionPane.showInputDialog(null,"输入登录账号");
            String inPwd = JOptionPane.showInputDialog(null,"输入登录密码");

            ATMMoreData codeObj = this.getCode(inCount);
            if (codeObj == null){
                JOptionPane.showMessageDialog(null,"账号不存在");
                continue;
            }

            if (codeObj.pwd.equals(inPwd)){
                JOptionPane.showMessageDialog(null,"登录成功");
                this.loginCount = codeObj;
                break;
            }
            JOptionPane.showMessageDialog(null,"登录账号或密码有误，重新输入");
            if (i == 2){
                JOptionPane.showMessageDialog(null,"非法用户");
                System.exit(0);
            }

//            String value = pro.getProperty(inCount);
//            String[] data = null;
//            if (value != null){
//                data = value.split(",");
//            }else {
//                JOptionPane.showMessageDialog(null,"账号不存在,重新输入");
//                continue;
//            }
//
//            if (data[0].equals(inPwd)){
//                JOptionPane.showMessageDialog(null,"登录成功");
//                this.loginCount =new ATMMoreData();
//                loginCount.count = inCount;
//                loginCount.pwd = inPwd;
//                loginCount.money = Integer.parseInt(data[1]);
//                break;
//            }
//            JOptionPane.showMessageDialog(null,"账号或密码有误，重新输入");
//            if (i == 2){
//                JOptionPane.showMessageDialog(null,"非法用户，账号冻结");
//                System.exit(0);
//            }
        }
    }

    //提取账号信息
    public ATMMoreData getCode(String code){
        String value = pro.getProperty(code);
        if (value == null){
            return null;
        }
        String[] data = null;
        data = value.split(",");
        ATMMoreData dataCode =new ATMMoreData();
        dataCode.count = code;
        dataCode.pwd = data[0];
        dataCode.money = Integer.parseInt(data[1]);
        return dataCode;
    }

    //存钱
    public void save(){
        int num =Integer.parseInt(JOptionPane.showInputDialog(null,"输入存款金额"));
        this.loginCount.money += num;
        this.saveFile();
        getInform();
    }

    //取钱
    public void getMoney(){
        int num = Integer.parseInt(JOptionPane.showInputDialog(null,"输入取款金额"));
        if (num>this.loginCount.money){
            JOptionPane.showMessageDialog(null,"余额不足");
            return;
        }
        JOptionPane.showMessageDialog(null,"取款成功");
        this.loginCount.money -= num;
        this.saveFile();
        this.getInform();
    }

    //转账
    public void transMoney(){
        String transCount = JOptionPane.showInputDialog(null,"输入汇款账号");
        ATMMoreData trans = this.getCode(transCount);
        if (trans==null){
            JOptionPane.showMessageDialog(null,"汇款账号不存在");
            return;
        }
        int num = Integer.parseInt(JOptionPane.showInputDialog(null,"输入转账金额"));
        if (num>this.loginCount.money){
            JOptionPane.showMessageDialog(null,"余额不足，无法汇款");
            return;
        }
        this.loginCount.money -= num;
        pro.setProperty(trans.count,trans.pwd+","+(trans.money+num));
        this.saveFile();
        this.getInform();
    }




    public void getInform(){
        JOptionPane.showMessageDialog(null,"当前余额为"+this.loginCount.money);
    }

    public void saveFile(){
        pro.setProperty(this.loginCount.count,this.loginCount.pwd+","+this.loginCount.money);
        try {
            pro.store(new FileWriter("Class8_25/src/ATMMore.txt"),null);
        }catch (Exception e){
            System.out.println("文件不存在");
        }
    }
}
